package com.softtown.config;

import com.softtown.license.license.LicenseVerify;
import de.schlichtherle.license.LicenseContentException;
import java.io.File;
import java.io.IOException;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @name: LicenseConfig
 * @author: tuacy.
 * @date: 2019/8/16.
 * @version: 1.0
 * @Description:
 */
@Configuration
public class LicenseConfig {

    /**
     * 证书subject
     */
    private String subject = "ioserver";

    /**
     * 公钥别称
     */
    private String publicAlias = "publicCert";

    /**
     * 访问公钥库的密码
     */
    private String storePass = "softtown@2020";


    @Bean(initMethod = "installLicense", destroyMethod = "unInstallLicense")
    public LicenseVerify licenseVerify() throws LicenseContentException, IOException {
//        // 获取当前jar包运行地址
//        ApplicationHome home = new ApplicationHome(getClass());
//        File jarFile = home.getSource();
//        String parent = jarFile.getParent();
        // 本地运行时的地址
        String parent = System.getProperty("user.dir");
        String licensePath = parent + File.separator + "license.lic";
        String publicKeysStorePath = parent + File.separator + "publicCerts.keystore";
        if (!new File(licensePath).exists() || !new File(publicKeysStorePath).exists()){
            throw new LicenseContentException("您的软件使用许可证书无效，请确认软件是否已取得授权1！");
        }
        return new LicenseVerify(subject, publicAlias, storePass,licensePath,publicKeysStorePath);
    }

}
